package com.zhouixi.listener;

import com.alibaba.fastjson.JSONObject;
import com.zhouixi.entity.Message;
import com.zhouixi.service.MessageStorageService;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

// 处理已读回执
@RocketMQMessageListener(consumerGroup = "im_ack_consumer", topic = "im_ack_topic")
@Component
public class AckListener implements RocketMQListener<String> {
    @Resource
    private MessageStorageService storageService;

    @Override
    public void onMessage(String message) {
        JSONObject ack = JSONObject.parseObject(message);
        if ("READ_ACK".equals(ack.getString("type"))) {
            String msgId = ack.getString("msgId");
            // 更新消息状态为已读
            storageService.updateMessageStatus(msgId, Message.MessageStatus.READ.getCode());
        }
    }
}